<?php
/**
* 
* Example integration of the ShareYourCart SDK
*
* Copyright (c) 2011 Barandi Solutions
* http://www.barandisolutions.ro
* 
**/
require_once('shareyourcart-sdk');
$SECRET_KEY = "................";    //write to office@barandisolutions.ro to obtain a partner key

/**
*
*  Send the cart
*
*/
function sendCart()
{
	//TODO: populate the params with either the cart's content, or the product the user is currently viewing
	$params = array();
	
	//TODO: get the app key, client id ( from the database )
	$settings = $wpdb->get_row("SELECT * FROM ".$wpdb->base_prefix."shareyourcart_settings LIMIT 1");
	$params['app_key'] = $settings->app_key;
	$params['client_id'] = $settings->client_id;
	
	//call the core SDK function
	$data = shareyourcart_startSessionAPI($params);
	
	//TODO: save the returned data, for later reference.
	//token and the session_id
	$wpdb->insert($wpdb->base_prefix."shareyourcart_tokens",$data);
}

/**
*
*  Validate the received coupon with ShareYourCart.com
*  Stops the php execution if the coupon is not valid
* 
*/
function saveCoupon()
{	
	//make sure the received coupon is valid.
	shareyourcart_ensureValidCouponAPI();
	
	//TODO: get session_id from database, according to the received TOKEN
	$tokens = $wpdb->get_row("SELECT session_id FROM ".$wpdb->base_prefix."shareyourcart_tokens WHERE token='".$_POST['token']."'");

	//make sure the token is valid
	if(empty($tokens))
	{
		header("HTTP/1.0 403");
		exit;
	}	
	
	//TODO: save the coupon in the db, so that it can be applied when the user checks out
	
	//check if the coupon is intended to be applied to the current cart
	if(empty($_POST['save_only']))
	{
		//restore the PHP session of the user for which this coupon has been created
		$session_id = $tokens->session_id;

		session_destroy();
		session_id($session_id);
		session_start();
		
		//TODO: apply the coupon to the current user's cart
	}
}
